package Client.View;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Server.Player;

import Client.Model.GameModel;

import logic.OnlinePlayer;

public class PlayerObject extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7115782284934298743L;
	
	OnlinePlayer op;
	GameModel model;
	
	public PlayerObject(OnlinePlayer op, GameModel model) {
		this.op = op;
		this.model = model;
		
		initialize();
	}
	
	
	private void initialize(){
		
		this.setBackground(LoginFrame.DEFAULT_BG_COLOR);

		this.add(new JLabel(op.username));

		
		switch (op.state){
		
		case Player.STATE_CONNECTED:
			this.add(new JLabel("[Connected]"));
			break;
		
		case Player.STATE_WAITING:
			this.add(new JLabel("[Waiting]"));
			break;
		
		case Player.STATE_PLAYING:
			this.add(new JLabel("[Playing]"));
			break;
		}
		

		if (op.state == Player.STATE_WAITING){
			
			JButton playButton = new JButton("Play!");
			this.add(playButton);

			playButton.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {

					model.playWith(op.username);

				}
			});

		}
		

		if (op.state != Player.STATE_PLAYING){

			if (model.gotPlayRequestFrom(op.username)){

				JButton acceptBtn = new JButton("Accept");
				this.add(acceptBtn);

				acceptBtn.addMouseListener(new MouseAdapter() {
					@Override
					public void mousePressed(MouseEvent e) {
						model.acceptInvite(op.username);
					}
				});
				
				JButton rejectBtn = new JButton("Reject");
				this.add(rejectBtn);

				rejectBtn.addMouseListener(new MouseAdapter() {
					@Override
					public void mousePressed(MouseEvent e) {
						model.rejectInvite(op.username);
					}
				});



			}
			else
				if (model.isPlayRequestSentTo(op.username)){
					
					JButton cancelInviteBtn = new JButton("Cancel");
					this.add(cancelInviteBtn);
					
					cancelInviteBtn.addMouseListener(new MouseAdapter() {
						@Override
						public void mousePressed(MouseEvent e) {
							
							model.cancelInvite(op.username);
							
						}
					});

				}
				else{

					JButton inviteButton = new JButton("Invite");
					this.add(inviteButton);

					inviteButton.addMouseListener(new MouseAdapter() {
						@Override
						public void mouseClicked(MouseEvent e) {

							model.invitePlayer(op.username);

						}
					});
				}
			
		}
		
		
	}
	
}
